package com.chuanyang.driverh.mainhome.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.bean.OrderStatusEntity;

import java.util.List;


/**
 * 相思无用，唯别而已。别期若有定，千般煎熬又何如，莫道黯然销魂，何处柳暗花明。
 * Created by sky on 2016/8/12.
 */
public class WaybilStatusAdapter extends BaseAdapter {

    private List<OrderStatusEntity> orderStatusEntities;
    private LayoutInflater mInflater;
    private Context mContext;

    public WaybilStatusAdapter(Context activity, List<OrderStatusEntity> orderStatusEntityList) {
        orderStatusEntities = orderStatusEntityList;
        mContext = activity;
        mInflater = LayoutInflater.from(activity);
    }

    @Override
    public int getCount() {
        return orderStatusEntities.size();
    }

    @Override
    public Object getItem(int position) {
        return orderStatusEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.home_item_order_status, null);
            holder = new ViewHolder();
            holder.iv_status = convertView.findViewById(R.id.iv_status);
            holder.tv_transport_status = (TextView) convertView.findViewById(R.id.tv_transport_status);
            holder.tv_transport_status_time = (TextView) convertView.findViewById(R.id.tv_transport_status_time);
            holder.tv_yichang = (TextView) convertView.findViewById(R.id.tv_yichang);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (orderStatusEntities.get(position).getWaybillBeforStatus() == "0" && orderStatusEntities.get(position).getWaybillBeforStatus() == "0"){
            holder.tv_transport_status.setText("开始发单");
        }   else if(orderStatusEntities.get(position).getWaybillBeforStatus() == "0" && orderStatusEntities.get(position).getWaybillAfterStatus() == "1"){
            holder.tv_transport_status.setText("订单已匹配到对应承运，等待部署运单");
        }else if (orderStatusEntities.get(position).getWaybillBeforStatus() == "0" && orderStatusEntities.get(position).getWaybillAfterStatus() == "2"){
            holder.tv_transport_status.setText("订单已匹配到对应承运，等待承运前往装货");
        } else if (orderStatusEntities.get(position).getWaybillBeforStatus() == "1" && orderStatusEntities.get(position).getWaybillAfterStatus() == "2"){
            holder.tv_transport_status.setText("运单已部署完毕，等待承运前往装货");
        } else if (orderStatusEntities.get(position).getWaybillBeforStatus() == "2" && orderStatusEntities.get(position).getWaybillAfterStatus() == "3"){
            holder.tv_transport_status.setText("承运正在装货");
        } else if (orderStatusEntities.get(position).getWaybillBeforStatus() == "3" && orderStatusEntities.get(position).getWaybillAfterStatus() == "4"){
            holder.tv_transport_status.setText("承运开始运输");
        } else if (orderStatusEntities.get(position).getWaybillBeforStatus() == "2" && orderStatusEntities.get(position).getWaybillAfterStatus() == "4"){
            holder.tv_transport_status.setText("承运开始运输");
        } else if (orderStatusEntities.get(position).getWaybillBeforStatus() == "4" && orderStatusEntities.get(position).getWaybillAfterStatus() == "5"){
            holder.tv_transport_status.setText("承运正在卸货");
        } else if (orderStatusEntities.get(position).getWaybillBeforStatus() == "5" && orderStatusEntities.get(position).getWaybillAfterStatus() == "6"){
            holder.tv_transport_status.setText("承运已确认卸货，等待货主收货");
        } else if (orderStatusEntities.get(position).getWaybillBeforStatus() == "4" && orderStatusEntities.get(position).getWaybillAfterStatus() == "6"){
            holder.tv_transport_status.setText("承运已确认卸货，等待货主收货");
        } else if (orderStatusEntities.get(position).getWaybillBeforStatus() == "6" && orderStatusEntities.get(position).getWaybillAfterStatus() == "7"){
            holder.tv_transport_status.setText("货主已确认收货、订单完成");
        } else if (orderStatusEntities.get(position).getWaybillBeforStatus() == "20" && orderStatusEntities.get(position).getWaybillAfterStatus() == "20"){
            holder.tv_transport_status.setText("已付款");
        }else if(Integer.parseInt(orderStatusEntities.get(position).getWaybillBeforStatus()) >= 0 &&
                Integer.parseInt(orderStatusEntities.get(position).getWaybillAfterStatus()) < 0 ){
            holder.tv_yichang.setVisibility(View.VISIBLE);
            holder.tv_yichang.setText("订单进入异常状态");
        } else if(Integer.parseInt(orderStatusEntities.get(position).getWaybillBeforStatus()) < 0 &&
                Integer.parseInt(orderStatusEntities.get(position).getWaybillAfterStatus()) >= 0 ){
            holder.tv_yichang.setVisibility(View.VISIBLE);
            holder.tv_yichang.setText("订单由异常状态恢复");
        }

//        if(orderStatusEntities.get(position).getChangerType() == "1"){
//            holder.tv_yichang.setVisibility(View.VISIBLE);
//            holder.iv_status.setBackgroundResource(R.drawable.shape_status_red_dot);
//        }
        holder.tv_transport_status_time.setText(orderStatusEntities.get(position).getUpdateTime());

        return convertView;
    }

    public class ViewHolder {
        public View iv_status;
        public TextView tv_transport_status;
        public TextView tv_transport_status_time;
        public TextView tv_yichang;
    }
}
